<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * File: models/data_model.php
 * 
 * @package Nested_sets
 * @subpackage Data
 */
include_once('nested_sets_model.php'); 
class Data_model extends Nested_sets_model 
{
    
    function Data_model() {
    
        parent::Model();
        
        // Initialise parameters to configure the Nested_sets model
        // to interact with the data table. This extension class
        // is the thing that defines the role of the nested sets model
        
        // The three parameters passed in here are:
        //  1) the table name
        //  2) the column name representing the "left value"
        //  3) the column name representing the "right value"
        $this->setControlParams('nsd','lft','rgt');
        
        // Here we tell the nested sets model which column is used as the
        // primary key. In our example, it's the auto_incrementing "id"
        // column in the categories table.
        $this->setPrimaryKeyColumn('id');
    }

    

    /**
     * Returns a form select / drop down.
     * Provide the form field name and this method will return an HTML snippet
     * suitable for using in a form
     * @param string $fieldname The form field name to use
     * @return string Html snippet appropriate for an HTML form
     */
    function getDataSelect($id, $selid = FALSE) 
    {
        $cats_handle = $this->getTreePreOrder($this->getNodeFromId($id));
        if(!empty($cats_handle['result_array'])) {

            $fieldname = $cats_handle['result_array'][0]['name'];
            $this->getTreeNext($cats_handle);
            $retVal = '<select name="'.$fieldname.">\n";
            if($selid === FALSE)$retVal .= '<option></option>'."\n";
            while($this->getTreeNext($cats_handle)) 
            {
                $sel = '';
                if($cats_handle['row']['id'] == $selid){
                    $sel = 'selected="selected"';
                }
                $retVal .= '<option '.$sel.' value="'.$cats_handle['row']['id'].'" \">"'.$cats_handle['row']['name']."</option>\n";
                
            }
        }
        
        $retVal .= "</select>\n";
        return $retVal;
    }


    
    /**
     * Returns a form select / drop down.
     * Provide the form field name and this method will return an HTML snippet
     * suitable for using in a form
     * @param string $fieldname The form field name to use
     * @return string Html snippet appropriate for an HTML form
     */
    function getAllDataSelect($fieldname) 
    {
        $retVal = "<select name=\"$fieldname\">\n";
        $cats_handle = $this->getTreePreOrder($this->getRoot());
        
        if(!empty($cats_handle['result_array'])) {
            while($this->getTreeNext($cats_handle)) 
            {
                // get indent value
                $indent = (str_repeat("&nbsp;", $this->getTreeLevel($cats_handle)*2));
                $retVal .= "<option value=\""  .$cats_handle['row']['id'] . "\">$indent".$cats_handle['row']['name']."</option>\n";
                
            }
        }
        
        $retVal .= "</select>\n";
        return $retVal;
    }
    
    /**
     * Overrides the nested_sets_model method to define the dataset field to
     * display in the tree by passing array("categoryname") to the getSubTree 
     * method
     * @return string html snippet representing the tree
     */
    function getTreeAsHTML()
    { 
        return $this->getSubTreeAsHTML($this->getRoot(), array('name','id'));
    }
}
?>
